package com.venusic.handwritedemo.slice;

import com.venusic.handwritedemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.*;

public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
//        MountState mountedStatus = DataUsage.getDiskMountedStatus();

        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        if (btn1 != null) {
            btn1.setClickedListener(component -> {
                Intent secondIntent = new Intent();
//                //指定待启动FA的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.venusic.handwritedemo")
                        .withAbilityName("com.venusic.handwritedemo.SecondAbility")
                        .build();
                secondIntent.setOperation(operation);
                //通过AbilitySlice的startAbility接口实现启动另一个页面
                startAbilityForResult(secondIntent, 1);
            });
        }

        Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
        if (btn2 != null) {
            btn2.setClickedListener(component -> {
                Intent secondIntent = new Intent();
                //指定待启动FA的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.venusic.handwritedemo")
                        .withAbilityName("com.venusic.handwritedemo.SecondAbility")
                        .build();
                secondIntent.setOperation(operation);
                secondIntent.setParam("isCrop", true);
                //通过AbilitySlice的startAbility接口实现启动另一个页面
                startAbilityForResult(secondIntent, 1);
            });
        }
    }


    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode==1&&resultData!=null){
                File file = new File(resultData.getStringParam("path"));
                //获取PixelMap
                ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
                sourceOptions.formatHint = "image/jpeg";
                ImageSource imageSource = null;
                try {
                    //图像解码
                    FileInputStream fis = new FileInputStream(file);
                    FileDescriptor fd = fis.getFD();
                    imageSource=ImageSource.create(fd,null);
                    PixelMap pm=imageSource.createPixelmap(null);

                    if (resultCode == 100) {
                        Image img1= (Image) findComponentById(ResourceTable.Id_img1);
                        img1.setPixelMap(pm);
                    } else if (resultCode == 101) {
                        Image img2= (Image) findComponentById(ResourceTable.Id_img2);
                        img2.setPixelMap(pm);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
